/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.farent.solutions.plugins.stripes.hyperstripes.core;

import com.farent.solutions.plugins.stripes.hyperstripes.spi.RenderKit;
import com.farent.solutions.plugins.stripes.hyperstripes.spi.WebLibrary;
import com.farent.solutions.plugins.stripes.hyperstripes.annotations.RenderKitNamespace;
import com.farent.solutions.plugins.stripes.hyperstripes.core.tags.StandardHTMLComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.tags.StandardHTMLTagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.spi.AbstractRenderKit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 *
 * @author users
 */
@RenderKitNamespace("http://www.farent.solutions/hyperstripes/ns/default/1")
public class HyperStripesRenderKit extends AbstractRenderKit implements RenderKit {

    private Map<Class, WebLibrary> webLibraries = new HashMap<Class, WebLibrary>();

    private HyperStripesRenderKit() {
        webLibraries.put(HyperStripesLibrary.class, new HyperStripesLibrary());
    }

    public static HyperStripesRenderKit getInstance() {
        return HyperStripesRenderKitSingleHolder.INSTANCE;
    }

    @Override
    public List<WebLibrary> getWebLibraries() {
        return new ArrayList<WebLibrary>(webLibraries.values());
    }

    private static class HyperStripesRenderKitSingleHolder {
        private static final HyperStripesRenderKit INSTANCE = new HyperStripesRenderKit();
    }

    /**
     * creates a model for the provided tagName. Each Model is different
     *
     * Also sets the ComponentHelper to ensure getComponentHelper does not return null
     * @param tagName
     * @return
     */
    @Override
    public ComponentModel getTagModelForTag( String tagName )
    {
        List<Class> classesWithAnnotation =
                ComponentAnnotationProcessor.getInstance().
                getClassesWithAnnotation( ModelProvider.class );
        ComponentModel model = null;
        for ( Class classWithAnnotation : classesWithAnnotation )
        {
            ModelProvider annotation = ( ModelProvider ) classWithAnnotation.getAnnotation(
                    ModelProvider.class );
            if( tagName.equalsIgnoreCase( annotation.value() ))
            {
                try
                {
                    model = ( ComponentModel ) classWithAnnotation.newInstance();
                }
                catch ( Exception ex )
                {
                    throw new RuntimeException( ex );
                }
                break;
            }
        }

        if( model == null && HTMLTagNames.isSupportedTag(tagName) )
        {
            model = new StandardHTMLComponentModel(tagName);
        }
        return model;
    }

    @Override
    public ComponentTagHandler getTagHandlerForTag( String tagName )
    {
        List<Class> classesWithAnnotation =
                ComponentAnnotationProcessor.getInstance().
                getClassesWithAnnotation( TagHandler.class );
        for ( Class classWithAnnotation : classesWithAnnotation )
        {
            TagHandler annotation = ( TagHandler ) classWithAnnotation.getAnnotation(
                    TagHandler.class );
            if( tagName.equalsIgnoreCase( annotation.value() ))
            {
                try
                {
                    return  ( ComponentTagHandler ) classWithAnnotation.newInstance();
                }
                catch ( InstantiationException ex )
                {
                    throw new RuntimeException( ex );
                }
                catch ( IllegalAccessException ex )
                {
                    throw new RuntimeException( ex );
                }
            }
        }
        return null;
    }
 }
